package com.jwcao.common.cast.operation.injection;

import com.jwcao.common.cast.annotation.CastField;
import com.jwcao.common.cast.operation.access.AccessOperation;
import com.jwcao.common.cast.operation.access.field.CastFieldAccessOperation;
import com.jwcao.common.cast.operation.access.field.FieldAccessOperation;
import com.jwcao.common.scan.object.ScanClass;
import java.util.List;

/**
 * name: InjectionOperation description: TODO
 *
 * @author caojingwei date: 2023/3/23 15:59
 */
public class FieldInjectionOperation extends InjectionOperation {

    private AccessOperation accessOperation;

    public FieldInjectionOperation(ScanClass targetClass, AccessOperation accessOperation) {
        super(targetClass);
        this.accessOperation = accessOperation;
    }

    public void execute(Object sourceObject, Object targetObject) {
        Object value = accessOperation.execute(sourceObject);
        accessOperation.getTargetField().set(targetObject, value);
    }

    public void flush() {
        if (accessOperation instanceof CastFieldAccessOperation fieldAccessOperation) {
            accessOperation = fieldAccessOperation.flush();
        }
    }
}
